home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Gfx / Edit / TSMorph / src / getbitmap.c < prev    next >
C/C++ Source or Header  |  1994-10-30  |  3KB  |  139 lines

  1. /*----------------------------------------------------------------------*
  2.  * GETBITMAP.C  Support routines for reading ILBM files.
  3.  * (IFF is Interchange Format File.)
  4.  *
  5.  * Based on code by Jerry Morrison and Steve Shaw, Electronic Arts.
  6.  * This software is in the public domain.
  7.  * Modified for iffparse.library by CBM 04/90
  8.  * This version for the Commodore-Amiga computer.
  9.  *----------------------------------------------------------------------*/
  10.  
  11. // This is basically the standard IFF source code with
  12. // minor changes to error handling (marked MJP)
  13. // The error codes are hard coded as no include file is included
  14.  
  15. #include "iffp/ilbm.h"
  16. #include "iffp/packer.h"
  17. #include "iffp/ilbmapp.h"
  18.  
  19. /* createbrush
  20.  *
  21.  * Passed an initialized ILBMInfo with a parsed IFFHandle (chunks parsed,
  22.  * stopped at BODY),
  23.  * gets the bitmap and colors
  24.  * Sets up ilbm->brbitmap, ilbm->colortable, ilbm->ncolors
  25.  * Returns 0 for success
  26.  */
  27. LONG createbrush(struct ILBMInfo *ilbm)
  28.     {
  29.     int error;
  30.  
  31.     error             = getbitmap(ilbm);
  32.     if(!error) error     = loadbody(ilbm->ParseInfo.iff,
  33.                         ilbm->brbitmap,&ilbm->Bmhd);
  34.     if(!error)         getcolors(ilbm);
  35.     if(error)          deletebrush(ilbm);
  36.     return(error);
  37.     }
  38.  
  39. /* deletebrush
  40.  *
  41.  * closes and deallocates created brush bitmap and colors
  42.  */
  43. void deletebrush(ilbm)
  44. struct ILBMInfo        *ilbm;
  45.     {
  46.     freebitmap(ilbm);
  47.     freecolors(ilbm);
  48.     }
  49.  
  50.  
  51. /* getbitmap
  52.  *
  53.  * Passed an initialized ILBMInfo with parsed IFFHandle (chunks parsed,
  54.  * stopped at BODY), allocates a BitMap structure and planes just large
  55.  * enough for the BODY.  Generally used for brushes but may be used
  56.  * to load backgrounds without displaying, or to load deep ILBM's.
  57.  * Sets ilbm->brbitmap.  Returns 0 for success.
  58.  */
  59. LONG getbitmap(struct ILBMInfo *ilbm)
  60.     {
  61.     struct IFFHandle    *iff;
  62.     BitMapHeader    *bmhd;
  63.     USHORT            wide, high;
  64.     LONG  error = NULL;
  65.     int k, extra=0;
  66.     BYTE deep;
  67.  
  68.     if(!(iff=ilbm->ParseInfo.iff))    return(CLIENT_ERROR);
  69.  
  70.     ilbm->brbitmap = NULL;
  71.  
  72.     if (!( bmhd = (BitMapHeader *)
  73.             findpropdata(iff, ID_ILBM, ID_BMHD)))
  74.         {
  75.         message(SI(MSG_IFFP_NOBMHD),NULL,2);    // MJP - 2 = HE_IFFBMHD
  76.         return(IFFERR_SYNTAX);
  77.         }
  78.  
  79.     *(&ilbm->Bmhd) = *bmhd;    /* copy contents of BMHD */
  80.  
  81.     wide = BitsPerRow(bmhd->w);
  82.     high = bmhd->h;
  83.     deep = bmhd->nPlanes;
  84.  
  85.     ilbm->camg = getcamg(ilbm);
  86.  
  87.     D(bug("allocbitmap: bmhd=$%lx wide=%ld high=%ld deep=%ld\n",
  88.             bmhd,wide,high,deep));
  89.     /*
  90.      * Allocate Bitmap and planes
  91.      */
  92.         extra = deep > 8 ? deep - 8 : 0;
  93.     if(ilbm->brbitmap = AllocMem(sizeof(struct BitMap) + (extra<<2),MEMF_CLEAR))
  94.         {
  95.         InitBitMap(ilbm->brbitmap,deep,wide,high);
  96.         for(k=0; k<deep && (!error); k++) 
  97.             {
  98.             if(!(ilbm->brbitmap->Planes[k] = AllocRaster(wide,high))) error = 1;
  99.             if(! error)
  100.             BltClear(ilbm->brbitmap->Planes[k],RASSIZE(wide,high),0);
  101.             }
  102.  
  103.         if(error)
  104.             {
  105.             message(SI(MSG_IFFP_NORASTER),NULL,3); // MJP 3 = HE_IFFRASTER
  106.             freebitmap(ilbm);
  107.             }
  108.         }
  109.     else error = 1;
  110.     return(error);
  111.     }
  112.  
  113.     
  114. /* freebitmap
  115.  *
  116.  * deallocates ilbm->brbitmap BitMap structure and planes 
  117.  */
  118. void freebitmap(struct ILBMInfo * ilbm)
  119.     {
  120.     int k, extra=0;
  121.  
  122.     if(ilbm->brbitmap)
  123.         {    
  124.         for(k=0; k< ilbm->brbitmap->Depth; k++) 
  125.             {
  126.             if(ilbm->brbitmap->Planes[k]) 
  127.                 FreeRaster(ilbm->brbitmap->Planes[k],
  128.                        (USHORT)(ilbm->brbitmap->BytesPerRow << 3),
  129.                        ilbm->brbitmap->Rows);
  130.             }
  131.  
  132.             extra = ilbm->brbitmap->Depth > 8 ? ilbm->brbitmap->Depth - 8 : 0;
  133.         FreeMem(ilbm->brbitmap,sizeof(struct BitMap) + (extra << 2));
  134.         ilbm->brbitmap = NULL;
  135.         }
  136.     }
  137.  
  138. /* end */
  139.